package com.spy.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spy.common.constant.HttpStatus;
import com.spy.common.core.page.TableDataInfo;
import com.spy.common.utils.DateUtils;
import com.spy.common.utils.StringUtils;
import com.spy.material.domain.Inventory;
import com.spy.material.mapper.InventoryMapper;
import com.spy.system.domain.MatCategory;
import com.spy.system.dto.MatMaterialPageReqDto;
import com.spy.system.mapper.MatCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spy.system.mapper.MatMaterialMapper;
import com.spy.system.domain.MatMaterial;
import com.spy.system.service.IMatMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 物料信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-09
 */
@Service
public class MatMaterialServiceImpl extends ServiceImpl<MatMaterialMapper, MatMaterial> implements IMatMaterialService
{
    @Autowired
    private MatMaterialMapper matMaterialMapper;

    /**
     * 查询物料信息
     * 
     * @param id 物料信息主键
     * @return 物料信息
     */
    @Override
    public MatMaterial selectMatMaterialById(Long id)
    {
                return getById(id);
    }

    /**
     * 查询物料信息列表
     * 
     * @param
     * @return 物料信息
     */
    @Override
    public TableDataInfo selectMatMaterialList(MatMaterialPageReqDto mpo)
    {
        Page page = new Page(mpo.getPageNum(), mpo.getPageSize());
        LambdaQueryWrapper<MatMaterial> wrapper=new LambdaQueryWrapper<>();
        if (mpo.getMaterialName()!=null && mpo.getMaterialName()!=" ") {
            wrapper.like(MatMaterial::getMaterialName, mpo.getMaterialName());
        }
        if (mpo.getStatus()!=null) {
            wrapper.eq(MatMaterial::getStatus, mpo.getStatus());
        }
        Page page1 = page(page, wrapper);
        TableDataInfo tableDataInfo = new TableDataInfo();

        List<MatMaterial> records = page1.getRecords();
        for (MatMaterial matMaterial : records) {
            MatCategory matCategory = matCategoryMapper.selectById(matMaterial.getCategoryId());
            matMaterial.setCategoryName(matCategory.getMaterialCategory());
        }

        tableDataInfo.setTotal(page1.getTotal());
        tableDataInfo.setRows(records);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    /**
     * 新增物料信息
     * 
     * @param matMaterial 物料信息
     * @return 结果
     */
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private MatCategoryMapper matCategoryMapper;
    @Override
    public int insertMatMaterial(MatMaterial matMaterial) {
    return save(matMaterial) ? 1 : 0;

    }

    /**
     * 修改物料信息
     * 
     * @param matMaterial 物料信息
     * @return 结果
     */
    @Override
    public int updateMatMaterial(MatMaterial matMaterial)
    {
        matMaterial.setUpdateTime(DateUtils.getNowDate());
                return updateById(matMaterial) ? 1 : 0;
    }

    /**
     * 批量删除物料信息
     * 
     * @param ids 需要删除的物料信息主键
     * @return 结果
     */
    @Override
    public int deleteMatMaterialByIds(Long[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除物料信息信息
     * 
     * @param id 物料信息主键
     * @return 结果
     */
    @Override
    public int deleteMatMaterialById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }
}
